<?php
namespace qulian\controllers;

use common\models\LianTypeModel;
use common\models\LianWebModel;
use Yii;
use qulian\base\BaseController;
use yii\data\Pagination;

/**
 * Site controller
 */
class SiteController extends BaseController
{
    public $menu = "";
    public $defaultAction = 'index';
    public function beforeAction($action)
    {
        if(parent::beforeAction($action)){
            $lian_type = parent::redisGet('lian_type',true);
            if(empty($index_novel)){
                $ids = [7,8,10,11,14,15,19,36,42,38,95,100,16,187];
                $lian_type = LianTypeModel::find()->select(['id','name','type'])->with(['subType'=>function($m){
                    return $m->select(['id','name','parent_id']);
                }])->where(['in','id',$ids])->asArray()->all();
                foreach ($lian_type as $k=>$item) {
                    $sub = '';
                    foreach ($item['subType'] as $key=>$val) {
                        if($key<=3){
                            $sub .= $val['name'].'  |  ';
                        }
                    }
                    unset($lian_type[$k]['subType']);
                    $lian_type[$k]['sub_name'] = mb_substr($sub,0,mb_strlen($sub)-3);
                }
                parent::redisSet('lian_type', $lian_type);
            }
            $new_web = parent::redisGet('new_web',true);
            if(empty($new_web)) {
                $new_web = LianWebModel::find()->select(['image', 'url', 'title', 'id','type'])->where(['status' => 1])->with(['typeName'=>function($m){
                    $m->select(['id','name']);
                }])->orderBy('create_time desc')->limit(10)->asArray()->all();
                parent::redisSet('new_web', $new_web);
            }
            $recommend_web = parent::redisGet('recommend_web',true);
            if(empty($recommend_web)) {
                $recommend_web = LianWebModel::find()->select(['image', 'url', 'title', 'id','type','auth_time'])->with(['typeName'=>function($m){
                    $m->select(['id','name']);
                }])->where(['status' => 1,'recommend'=>1])->orderBy('create_time desc')->limit(10)->asArray()->all();
                foreach ($recommend_web as $k=>$item) {
                    $recommend_web[$k]['auth_time'] = date('Y-m-d H:i:s',$item['auth_time']);
                }
                parent::redisSet('recommend_web', $recommend_web);
            }
            $vip_web = parent::redisGet('vip_web',true);
            if(empty($vip_web)) {
                $vip_web = LianWebModel::find()->select(['image', 'url', 'title', 'id','type','auth_time'])->with(['typeName'=>function($m){
                    $m->select(['id','name']);
                }])->where(['status' => 1])->andWhere(['!=','auth_time',''])->orderBy('create_time desc')->limit(12)->asArray()->all();
                foreach ($vip_web as $k=>$item) {
                    $vip_web[$k]['auth_time'] = date('Y-m-d H:i:s',$item['auth_time']);
                }
                parent::redisSet('vip_web', $vip_web,60*61);
            }
            return $this->menu = $lian_type;
        }
        return false;
    }

    /**
     * 首页
     * @return string
     */
    public function actionIndex()
    {
        Yii::$app->layout = 'main';
        $recommend_web = parent::redisGet('recommend_web',true);
        $new_web = parent::redisGet('new_web',true);
        $vip_web = parent::redisGet('vip_web',true);
        return $this->render('index',[
            'recommend_web'=>$recommend_web,
            'new_web'=>$new_web,
            'vip_web'=>$vip_web,
        ]);
    }

    /*
     * 列表
     */
    public function actionList()
    {
        Yii::$app->layout = 'main';
        $data = $this->body;
        $recommend_web = parent::redisGet('recommend_web',true);
        $vip_web = parent::redisGet('vip_web',true);
        $type = LianTypeModel::find()->andWhere(['type'=>0])->asArray()->all();
        $son_type = $sun_type = [];
        $sun_id = $son_id = $top_id = '';
        $model = LianWebModel::find();
        $condition = [];
        if(!empty($data['id'])) {
            $this_type = LianTypeModel::find()->andWhere(['id'=>$data['id']])->asArray()->one();
            if ($data['type'] == 0) {
                $son_type = parent::redisGet($data['id'] . '_1_type_list', true);
                if (empty($son_type)) {
                    $son_type = LianTypeModel::find()->andWhere(['type' => 1, 'parent_id' => $data['id']])->asArray()->all();
                    parent::redisSet($data['id'] . '_1_type_list', $son_type, 30 * 24 * 60 * 60);
                }
                $son_type_id = parent::redisGet($data['id'] . '_sub_type_list', true);
                if (empty($son_type_id)) {
                    $son_type_id = LianTypeModel::find()->andWhere(['type' => 2])->andWhere(['in', 'parent_id', array_column($son_type, 'id')])->asArray()->all();
                    parent::redisSet($data['id'] . '_sub_type_list', $son_type_id, 30 * 24 * 60 * 60);
                }
                $top_id = $data['id'];
                $condition = ['in', 'type', array_column($son_type_id, 'id')];
            }
            if ($data['type'] == 1) {
                $top_id = $this_type['parent_id'];
                $son_id = $data['id'];
                $son_type = parent::redisGet($this_type['parent_id'] . '_1_type_list', true);
                if (empty($son_type)) {
                    $son_type = LianTypeModel::find()->andWhere(['type' => 1, 'parent_id' => $this_type['parent_id']])->asArray()->all();
                    parent::redisSet($this_type['parent_id'] . '_1_type_list', $son_type, 30 * 24 * 60 * 60);
                }
                $sun_type = parent::redisGet($data['id'] . '_2_type_list', true);
                if (empty($sun_type)) {
                    $sun_type = LianTypeModel::find()->andWhere(['type' => 2, 'parent_id' => $data['id']])->asArray()->all();
                    parent::redisSet($data['id'] . '_2_type_list', $sun_type, 30 * 24 * 60 * 60);
                }
                $condition = ['in', 'type', array_column($sun_type, 'id')];
            }
            if ($data['type'] == 2) {
                $sun_type = parent::redisGet($data['id'] . '_2_type_list', true);
                if (empty($sun_type)) {
                    $sun_type = LianTypeModel::find()->andWhere(['type' => 2, 'parent_id' => $this_type['parent_id']])->asArray()->all();
                    parent::redisSet($data['id'] . '_2_type_list', $sun_type, 30 * 24 * 60 * 60);
                }
                $son_type = parent::redisGet($sun_type[0]['parent_id'] . '_1_type_list', true);
                if (empty($son_type)) {
                    $son_type_id = LianTypeModel::find()->andWhere(['id' => $sun_type[0]['parent_id']])->one();
                    $son_type = LianTypeModel::find()->andWhere(['type' => 1, 'parent_id' => $son_type_id->parent_id])->asArray()->all();
                    parent::redisSet($son_type[0]['parent_id'] . '_1_type_list', $son_type, 30 * 24 * 60 * 60);
                }
                $top_id = $son_type[0]['parent_id'];
                $son_id = $sun_type[0]['parent_id'];
                $sun_id = $data['id'];
                $condition = ['type' => $data['id']];
            }
            $model->where($condition);
        }
        if(!empty($data['keyword'])){
            $model->where(['like','title',$data['keyword']]);
        }
        $count = $model->count();
        $offset = ($data['page']-1)*40;
        $model = $model->offset($offset)->limit(10)->all();
        return $this->render('list',[
            'recommend_web'=>$recommend_web,
            'vip_web'=>$vip_web,
            'type'=>$type,
            'son_type'=>$son_type,
            'sun_type'=>$sun_type,
            'top_id'=>$top_id,
            'son_id'=>$son_id,
            'sun_id'=>$sun_id,
            'model'=>$model,
            'page' => new Pagination(['totalCount' =>$count, 'pageSize' => 40, 'pageSizeParam'=>false]),
            'count'=>$count
        ]);
    }


    /*
     * 详情页
     */
    public function actionInfo()
    {
        Yii::$app->layout = 'main';
        $data = $this->body;
        $recommend_web = parent::redisGet('recommend_web',true);
        $info = LianWebModel::find()->where(['id'=>$data['id']])->one();
        $type_all = LianWebModel::find()->where(['type'=>$info->type,'status'=>1])->andWhere(['!=','id',$info->id])->with(['typeName'=>function($m){
            $m->select(['id','name']);
        }])->limit(10)->asArray()->all();
        $vip_web = parent::redisGet('vip_web',true);
        return $this->render('preview',[
            'recommend_web'=>$recommend_web,
            'info'=>$info,
            'type_all'=>$type_all,
            'vip_web'=>$vip_web,
        ]);
    }
}

